import { createSlice } from "@reduxjs/toolkit"

const cart = createSlice({
    name: 'cart',
    initialState: {
        UserMessage: {},
        collectGoodList: [],
        collectBusinessList:[],
        historyList:[],
        ids:[],
        bz:[],
        dailist:[],
        yifklist:[],
    },

    reducers: {
        putUserMessage(state, actions) {
            state.UserMessage = { ...actions.payload }
        },
        DelGoodCollect(state, actions) {
            state.collectGoodList = state.collectGoodList.filter(item => item._id !== actions.payload._id)
        },
        AddGoodCollect(state,actions){
            console.log(actions.payload);
            state.collectGoodList = [...state.collectGoodList,actions.payload]
        },
        DelBusinessCollect(state, actions) {
            state.collectBusinessList = state.collectBusinessList.filter(item => item._id !== actions.payload._id)
        },
        AddBusinessCollect(state,actions){
            console.log(actions.payload);
            state.collectBusinessList = [...state.collectBusinessList,actions.payload]
        },
        addList(state,actions) {

            let index = state.historyList.findIndex(item => item._id === actions.payload._id)
            if(index==-1){
                 state.historyList.push({...actions.payload,counts:1})
                 state.ids.push(actions.payload._id)
            }else{
                state.historyList[index].counts++
            }
           
        },
        jianList(state,actions){
            let index = state.historyList.findIndex(item => item._id === actions.payload._id)
            state.historyList[index].counts--
            if(state.historyList[index].counts==0){
                state.historyList.splice(index,1)
                state.ids.splice(index,1)
            }
        },
        //提交订单
        addDai(state,actions){
            let obj =[]
            state.historyList.forEach(item => {
                obj.push(item)
            })
            obj.push(actions.payload)
            console.log(obj);
            
            state.dailist=obj
            // state.dailist.push(actions.payload)
            // console.log(actions.payload);
            state.historyList=[]
            
        },
        //添加备注
        addbz(state,actions){

        }

    }
})

export const { putUserMessage, DelGoodCollect ,AddGoodCollect,DelBusinessCollect, AddBusinessCollect,addList, addNum,jianList,addbz,addDai} = cart.actions
export default cart.reducer
